﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SecurityLib;
using System.Data;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for MemberShip_ASP
/// </summary>
public class MemberShip_ASP
{
    //khai bao cac class
    public class QueryAll
    {
        public string MaDoanVien { get; set; }
        public string TenDoanVien { get; set; }
        public string TenDonVi { get; set; }
        public string Phai { get; set; }
        public string NgaySinh { get; set; }
        public string CMND { get; set; }
        public string SoTheDoan { get; set; }
        public int? TrangThai { get; set; }
    }
    public class Query_tuybien
    {
        public string MaDoanVien { get; set; }
        public string TenDoanVien { get; set; }
        public string GioiTinh { get; set; }
        public string NgaySinh { get; set; }
        public string NgayVaoDoan { get; set; }
        public string NguyenQuan { get; set; }
    }
    public class LocDoanVien
    {
        public int? ID { get; set; }
        public string MaDoanVien { get; set; }
        public string TenDoanVien { get; set; }
        public string sothedoan { get; set; }
     
        public string Phai { get; set; }
        public string NgaySinh { get; set; }
        public double TuoiTac { get; set; }
        public double NgaySinhTheoSo { get; set; }
        public string NgayVaoDoan { get; set; }
        public string NguyenQuan { get; set; }
        public string CMND { get; set; }
        public string SoTheDoan { get; set; }
        public int? TrangThai { get; set; }
        public string DienThoai { get; set; }
        public string Email { get; set; }
        public int? Dan_toc { get; set; }
        public int? Ton_Giao { get; set; }
        public string Trinh_do_Van_Hoa { get; set; }
        public string MaDonViTrucThuoc { get; set; }
        public int? trang_thai { get; set; }
        public int? Danhgia { get; set; }
        public LocDoanVien()
        {
           

        }

    }

    public class Doanvien_TheDoan
    {
        public string MaDoanVien { get; set; }
        public string TenDoanVien { get; set; }
        public string sothedoan { get; set; }
        public string so_sodoan { get; set; }
        public bool? capthe_capso{get;set;}
        public DateTime ngaycapthe { get; set; }
        public string Phai { get; set; }
        public string NgaySinh { get; set; }
        public double TuoiTac { get; set; }
        public double NgaySinhTheoSo { get; set; }
        public string NgayVaoDoan { get; set; }
        public string NguyenQuan { get; set; }
        public string CMND { get; set; }
        public string SoTheDoan { get; set; }
        public int? TrangThai { get; set; }
        public string DienThoai { get; set; }
        public string Email { get; set; }
        public int? Dan_toc { get; set; }
        public int? Ton_Giao { get; set; }
        public string Trinh_do_Van_Hoa { get; set; }
        public string MaDonViTrucThuoc { get; set; }
        public int? trang_thai { get; set; }
        public int? Danhgia { get; set; }
        public Doanvien_TheDoan()
        {
           

        }
    }
    public class LocCosodoan
    {
        
        public string MaCSD { get; set; }
        public string Tendonvi { get; set; }
        public string NgayThanhLap { get; set; }
        public string Email { get; set; }
        public string SDT { get; set; }
        public string DonViTrucThuoc { get; set; }
        public int DoanKhoi { get; set; }
        public int LoaiDonvi { get; set; }
        public string DiaChi { get; set; }
        public int Xeploai { get; set; }
        public LocCosodoan()
        {


        }

    }

    //khai bao class cho news
    public class ItemNews
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string NguoiDang { get; set; }
        public string NgayDang { get; set; }
        public int? Loai { get; set; }
    }

    public class ItemMaDoanVien
    {
        public string MaDoanVien { get; set; }
    }
    //public static string khoangthoigia = "";
    //public static string tendonvi = "";
    //so ngay trong khoang thoi gian
    //public static int number_of_day=0;   
    //cac ham xu ly
    public static string[] GetRoles(string UserName)
    {
        _dbDataContext data = new _dbDataContext();
        var tablerole = from p in data.tblUserInRoles
                        where p.tblAcount.TenDangNhap.ToString().Trim().ToUpper() == UserName.ToUpper() && p.PhanQuyen==true
                        select new
                        {
                            rolename = p.tblRole.RoleName.ToString().Trim()
                        };
        string[] Roles = new string[tablerole.ToList().Count];
        int i = 0;
        foreach (var role in tablerole)
        {
            Roles[i] = role.rolename.ToString().Trim();
            i++;
        }
        return Roles;
    }
    public static bool Login(string UserName, string Password)
    {
        _dbDataContext data = new _dbDataContext();
        PasswordHasherEncrypt passhasher = new PasswordHasherEncrypt();
        var login = from p in data.tblAcounts
                    where (p.TenDangNhap.ToString().Trim().ToUpper() == UserName.ToUpper() && p.MaKhau.Trim() == passhasher.Hash_SHA1(passhasher.EncryptPassword_MD5(Password)) && p.PhanQuyen == true)
                    select p;
        if (login.ToList().Count > 0)
            return true;
        else
            return false;
    }
    public static string GetMaDonViFromUserName(string username)
    {
        _dbDataContext _db = new _dbDataContext();
        string madonvi = _db.tblAcounts.Single(r=>r.TenDangNhap.Trim().ToUpper()==username.ToUpper()).DonVi.Trim();
        return madonvi;
    }
    public static string GetDisplayName(string username)
    {
        _dbDataContext _db = new _dbDataContext();
        string tenhienthi = _db.tblAcounts.Single(r => r.TenDangNhap.Trim().ToUpper() == username.ToUpper()).TenHienThi.Trim();
        return tenhienthi;
    }
    public static bool CheckOldPassWithUserName(string username, string oldpassinput)
    {
        _dbDataContext _db = new _dbDataContext();
        PasswordHasherEncrypt passhasher = new PasswordHasherEncrypt();
        var user = _db.tblAcounts.Single(r => r.TenDangNhap.Trim() == username);
        if (user.MaKhau.Trim() == passhasher.Hash_SHA1(passhasher.EncryptPassword_MD5(oldpassinput)))
            return true;
        else
            return false;
    }
    public static int GetUserIDOfUserLogIn(string username)
    {
        _dbDataContext _db = new _dbDataContext();
        return _db.tblAcounts.Single(r => r.TenDangNhap.Trim() == username.Trim()).ID;
    }
    public static void BindDrop(DropDownList drl,string usernamelogin)
    {
        ListItem item;
        item = new ListItem("---Chọn Đơn vị---", "0");
        drl.Items.Add(item);
        _dbDataContext data = new _dbDataContext();
        string madonvi = MemberShip_ASP.GetMaDonViFromUserName(usernamelogin);//lay ma don vi thuoc username dang nhap
        var d = from p in data.tblCoSoDoans where ((p.TrucThuoc.Trim() == madonvi.Trim() || p.MaDonVi.Trim() == madonvi) && p.GiaiThe == false) select p;
        foreach (var r in d)
        {
            item = new ListItem(r.TenDonVi.Trim(), r.MaDonVi.Trim());
            drl.Items.Add(item);
        }
    }
}